package com.windystory.wslibrary.base.activity;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.windystory.wslibrary.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 刷新
 */
public abstract class BaseRefreshActivity<T> extends BaseActivity {

    public SmartRefreshLayout refresh;
    public RecyclerView recycleView;
    public BaseQuickAdapter adapter;
    public int page = 1;
    public LinearLayout linear_loading;
    public LinearLayout layout_toolbar;
    public List<T> dataList = new ArrayList<>();
    public LinearLayout linear_error;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_base_refresh;
    }

    @Override
    protected void initView() {
        refresh = (SmartRefreshLayout) findViewById(R.id.refreshLayout);
        recycleView = (RecyclerView) findViewById(R.id.recyclerView);
        linear_loading = (LinearLayout) findViewById(R.id.linear_skeleton);
        linear_error = (LinearLayout) findViewById(R.id.linear_error);
        layout_toolbar = (LinearLayout) findViewById(R.id.layout_toolbar);
        initTitle();
    }

    /**
     * 特殊初始化
     */
    protected void initTitle() {
        Objects.requireNonNull(getSupportActionBar()).hide();
        View v = LayoutInflater.from(this).inflate(initTitleLayout(), null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layout_toolbar.addView(v, lp);
    }

    /**
     * 初始化Title Layout布局
     *
     * @return
     */
    protected abstract int initTitleLayout();


    @Override
    protected void initData() {
        dataList = setList();
        adapter = setAdapter(dataList);
        refresh.setEnableAutoLoadMore(true);
        recycleView.setLayoutManager(setLayoutManager());
        recycleView.setAdapter(adapter);
        adapter.bindToRecyclerView(recycleView);
        adapter.setEmptyView(setEmptyView());

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getData(page);
            }
        });

        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData(page);
            }
        });

        getData(page);
    }


    protected abstract List<T> setList();

    /**
     * 设置空状态layout
     */
    protected abstract int setEmptyView();

    @Override
    protected void initListener() {
        initCustomOpration();
    }

    /**
     * 其他初始化等操作
     */
    protected abstract void initCustomOpration();


    /**
     * 设置LayoutManager
     *
     * @return
     */
    protected abstract RecyclerView.LayoutManager setLayoutManager();

    /**
     * 设置适配器
     *
     * @param ts
     * @return
     */
    protected abstract BaseQuickAdapter setAdapter(List<T> ts);

    /**
     * 获取数据
     *
     * @param page
     */
    protected abstract void getData(int page);

    /**
     * 显示网络错误
     */
    private void showErrorLayout() {
        linear_error.setVisibility(View.VISIBLE);
    }

    /**
     * 不显示网络错误
     */
    private void dissmissErrorLayout() {
        linear_error.setVisibility(View.GONE);
    }

    @Override
    public void withOutNetWork() {
        showErrorLayout();
    }

    @Override
    public void withMobileNetWork() {
        dissmissErrorLayout();
    }

    @Override
    public void withWiFiNetWork() {
        dissmissErrorLayout();
    }

    /**
     * 完成刷新
     */
    public void onLoadData2Finish() {
        if (page == 1) {
            refresh.finishRefresh();
        } else {
            refresh.finishLoadMore();
        }
    }

}
